1 =============================================================================
2 WF4/WCF APPLICATION : CSWF4ServiceHostFactory
3 =============================================================================
4 /////////////////////////////////////////////////////////////////////////////
7 By using WorkflowServiceHost class, we can create a WF4 service host in code.
8 The advantage of using WorkflowServiceHost is that we can add our own workflow
9 extensions, tracking participant and persistence store.
11 So question is: can we use our own WorkflowServiceHost in IIS7 like we did in
12 the console application? This sample is for answering this question.
15 1. Open CSWF4ServiceHostFactory.sln with Visual Studio 2010
19 /////////////////////////////////////////////////////////////////////////////
27 /////////////////////////////////////////////////////////////////////////////
30 1.Create a Workflow Service project named CSWF4ServiceHostFactory.
31 2.Add assembly references to:
32 System.Workflow.Runtime
33 System.Workflow.Activities
34 System.ServiceModel.Activation
36 System.Activities.DurableInstancing
37 System.Runtime.DurableInstancing
38 3.Setup WF4 sql persistence store.
39 http://msdn.microsoft.com/en-us/library/ee395773.aspx
40 name the persistence database:WF4PersistenceDB.
41 4.add the following configuration to web.config file right under <configuration> node.
43 <add key="SqlWF4PersistenceConnectionString"
44 value="Data Source=.\sqlexpress;Initial Catalog=WF4PersistenceDB;Integrated Security=True" />
46 5.Create a code file named MyServiceHostFactory.cs
48 using System.Collections.Generic;
49 using System.Workflow.Activities;
50 using System.Activities;
51 using System.Workflow.Runtime;
52 using System.ServiceModel.Activities.Activation;
53 using System.ServiceModel.Activities;
54 using System.ServiceModel.Description;
55 using System.Activities.DurableInstancing;
56 using System.Configuration;
58 namespace CSWF4ServiceHostFactory
60 public class MyServiceHostFactory :
61 System.ServiceModel.Activities.Activation.WorkflowServiceHostFactory
63 protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service,
66 WorkflowServiceHost host =
67 base.CreateWorkflowServiceHost(service, baseAddresses);
68 string connectionString =
69 ConfigurationManager.AppSettings["SqlWF4PersistenceConnectionString"].ToString();
70 SqlWorkflowInstanceStore instanceStore =
71 new SqlWorkflowInstanceStore(connectionString);
72 instanceStore.InstanceCompletionAction =
73 InstanceCompletionAction.DeleteNothing;
74 host.DurableInstancingOptions.InstanceStore = instanceStore;
80 6.Open web.config file and add a ServiceHostingEnvironment node under the
82 <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
84 <add relativeAddress="~/Service1.xamlx"
85 service="Service1.xamlx"
86 factory="CSWF4ServiceHostFactory.MyServiceHostFactory"/>
88 </serviceHostingEnvironment>
89 7.Open the default created Service1.xaml, select SendResponse activity
90 check its PersistBeforeSend property.
91 8.Build the project and then create a IIS7 WebSite(Framework 4.0), map the physical path
93 Note: To prevent that the default Application Pool identity have no permission to access the
94 Web.config file and database, you can try shift the identity to LocalSystem.
95 9.Call Service1.xamlx. you can call it in WcfTestClient.exe.(usually, you can
96 find WcfTestClient.exe in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE)
99 /////////////////////////////////////////////////////////////////////////////
102 http://xhinker.com/post/WF4Create-Your-Own-ServiceHostFactory.aspx